package com.cyy.controller;

import com.cyy.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author CYY
 * @since 2022/5/18
 */
@Controller
public class UserController {

    //请求参数与形参名相同
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name,int age){
        System.out.println("普通参数传递 name -==》 "+name);
        System.out.println("普通参数传递 age -==》 "+age);
        return "{'module':'common Param'}";
    }

    //请求参数名称与形参名不同
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    public String commonParamDifferentName(@RequestParam("name") String userName, int age){
        System.out.println("普通参数传递 userName -==》 "+userName);
        System.out.println("普通参数传递 age -==》 "+age);
        return "{'module':'common Param Different Name'}";
    }

    //pojo参数
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println("pojo参数传递 user -==》 "+user);
        return "{'module':'pojo param'}";
    }

    //嵌套pojo参数
    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(User user){
        System.out.println("pojo嵌套pojo参数传递 user -==》 "+user);
        return "{'module':'pojo Contain Pojo param'}";
    }

    //数组参数
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes){
        System.out.println("数组参数传递 likes -==》 "+ Arrays.toString(likes));
        return "{'module':'array Param'}";
    }

    //集合参数
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes){
        System.out.println("集合参数传递 likes -==》 "+ likes);
        return "{'module':'list Param'}";
    }


    //集合参数：json格式
    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes){
        System.out.println("list common(json)参数传递 likes -==》 "+ likes);
        return "{'module':'list common for json param'}";
    }

    //POJO参数：json格式
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user){
        System.out.println("pojo(json)参数传递 user -==》 "+ user);
        return "{'module':'pojo for json param'}";
    }

    //集合参数：json格式
    @RequestMapping("/listPojoParamForJson")
    @ResponseBody
    public String listPojoParamForJson(@RequestBody List<User> list){
        System.out.println("list pojo(json)参数传递 list -==》 "+ list);
        return "{'module':'list pojo for json param'}";
    }

    //日期参数
    @RequestMapping("/dateParam")
    @ResponseBody
    public String dateParam(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date2){
        System.out.println("参数传递 date -==》 "+ date);
        System.out.println("参数传递 date1(yyyy-MM-dd) -==》 "+ date1);
        System.out.println("参数传递 date2(yyyy-MM-dd HH:mm:ss) -==》 "+ date2);
        return "{'module':'date param'}";
    }

}
